package com.xzz.thread.demo;

import java.util.ArrayList;
import java.util.List;

public class Warehouse {

    private static int MAX_VALUE = 100;

    private List<Integer> list = new ArrayList<>();

    //生产
    public void product(int num) {
        synchronized (list){
            while (list.size() + num > MAX_VALUE) {
                System.out.println("暂时不能生产"+num+"个");
                try {
                    list.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int i= 0 ;i<num;i++){
                list.add(1);
            }
            System.out.println("生产了"+num+"个,共有"+list.size()+"个");
            list.notifyAll();
        }

    }
    //消费
    public void consume(int num){
        synchronized (list){
            while(list.size()<num){
                System.out.println("暂时不能消费"+num+"个");
                try {
                    list.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int i= 0 ;i<num;i++){
                list.remove(0);
            }
            System.out.println("消费了"+num+"个,共有"+list.size()+"个");
            list.notifyAll();
        }

    }
}
